﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;

namespace QLTB_CTK34
{
    public partial class FrmDangNhap : Form
    {
        public FrmDangNhap()
        {
            InitializeComponent();
            this.MaximizeBox = false;
            
        }
        public bool LoginSuccess = false;
        private void btnLogin_Click(object sender, EventArgs e)
        {
            MyConnectString.GetMyConnectString = string.Format("Data Source={0};Initial Catalog={1};User Id= {2};Password= {3};", 
                Properties.Settings.Default["ServerName"],
                Properties.Settings.Default["DatabaseName"],
                Properties.Settings.Default["UserID"], 
                Properties.Settings.Default["PassWord"]);
            IEnumerable<TaiKhoan> kt = null;
            ThuVien.TRY_CATCH(() =>
            {
                kt = QLTaiKhoan.FindByCondition(x => x.TenDangNhap.Trim() == txtUsername.Text && x.MatKhau.Trim() == txtPassword.Text);
            });
            if (kt !=null&&kt.Count() > 0)
            {
                LoginSuccess = true;
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("tên đăng nhập hoặc mật khẩu không hợp lệ");
                txtUsername.Focus();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel; 
            Application.Exit();

        }

        private void FrmDangNhap_FormClosing(object sender, FormClosingEventArgs e)
        {
             this.DialogResult = DialogResult.Cancel;
        }

        private void FrmDangNhap_Load(object sender, EventArgs e)
        {
            txtUsername.Focus(); 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmKetNoiCSDL frmkncsdl = new FrmKetNoiCSDL();
            frmkncsdl.ShowDialog();
        }
    }
}
